<?php
namespace app\common\business;

class SpecsValue extends BusBase {
    public $model = null;

    public function __construct()
    {
        $this->model = new \app\common\model\mysql\SpecsValue();
    }
    public function getBySpecsId($specsId) {
        try {
            $result = $this->model->getNormalBySpecsId($specsId, 'id,name');
        } catch (\Exception $e) {
            return [];
        }
        return $result->toArray();
    }

    public function dealGoodsSkus($gids){
        $specsValueKeys = array_keys($gids);
        foreach ($specsValueKeys as $specsValueKey){
            $specsValueKey = explode(',', $specsValueKey);
            foreach ($specsValueKey as $k=>$v){
                $new[$k][] = $v;
                $specsValueIds[] = $v;
            }
        }
        $specsValueIds = array_unique($specsValueIds);
        $specsValues = $this->getNormalInIds($specsValueIds);
    }

    private function getNormalInIds(array $specsValueIds)
    {
        if (empty($ids)){
            return [];
        }
        try {
            $result = $this->model->getNormalInIds($ids);
        } catch (\Exception $e) {
            return [];
        }
        $result = $result->toArray();
        if (empty($result)){
            return [];
        }
        $specsNames = config('specs');
        $specsNamesArrs = array_column($specsNames, 'name', 'id');
        foreach ($result as $resultValue){
            $res[$resultValue['id']] = [
                'name'=>$resultValue['name'],
                'specs_name'=>$specsNamesArrs[$resultValue['specs_id']] ?? ''
            ];
        }
        return $res;
    }
}